from common.functions import log4gs


@log4gs
class VueMessage:
    """vue框架界面上方的提示信息"""

    messageClassName = 'ivu-message'
    noticeClassName = 'ivu-message-notice'
    mapping = {
        'success': 'ivu-message-success',   # 绿色打勾的提示
        'warning': 'ivu-message-warning',   # 黄色感叹号的提示
        'error': 'ivu-message-error'        # 红色打叉的提示
    }

    def __init__(self, driver):
        self.driver = driver
        try:
            self.msg_box = self.driver.find_element_by_class_name(self.messageClassName)
        except Exception:
            self.log.error("页面还没有出现过提示信息", exc_info=True)

    def __get__(self, instance, owner):
        # self.driver = instance.driver
        # try:
        #     self.msg_box = self.driver.find_element_by_class_name(self.messageClassName)
        # except Exception:
        #     self.log.error("页面还没有出现过提示信息", exc_info=True)
        return self

    def __factory(self, type_=None):
        if type_:
            try:
                div = self.msg_box.find_element_by_class_name(self.mapping[type_])
                msg = div.find_element_by_tag_name('span').text.strip()
                return msg
            except Exception:
                self.log.error("没有找到'{}'类型的提示".format(type_), exc_info=True)
        else:
            try:
                msg = self.msg_box.find_element_by_class_name(self.noticeClassName).text.strip()
                return msg
            except Exception:
                self.log.error("没有任何提示", exc_info=True)
        return None

    @property
    def msg(self):
        return self.__factory()

    @property
    def success_msg(self):
        return self.__factory(type_='success')

    @property
    def warning_msg(self):
        return self.__factory(type_='warning')

    @property
    def error_msg(self):
        return self.__factory(type_='error')

    def has_msg(self):
        return bool(self.msg)

    def is_success(self):
        return bool(self.success_msg)

    def is_warning(self):
        return bool(self.warning_msg)

    def is_error(self):
        return bool(self.error_msg)